/******************************************************************************
 *    Copyright 2013 IT2media - IT-Solutions for Directory Business           *
 *                                                                            *
 *  Licensed under the Apache License, Version 2.0 (the "License");           *
 *  you may not use this file except in compliance with the License.          *
 *  You may obtain a copy of the License at                                   *
 *                                                                            *
 *      http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 ******************************************************************************/
package de.it2media.javasitemapgenerator;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.it2media.javasitemapgenerator.header.XMLNS;
import de.it2media.javasitemapgenerator.url.JavaSitemapGeneratorURL;

public abstract class AbstractJavaSitemapGenerator implements JavaSitemapGenerator {

  /**
   * A sex logger for our app
   */
  protected static Logger             log              = LoggerFactory.getLogger(AbstractJavaSitemapGenerator.class);

  /**
   * The output file encoding
   * Default is Charset.defaultCharset()
   * but you can set what you want
   * This is needed to write the the first xml line
   * in most case, this should be UTF-8
   */
  protected static Charset            fileEncoding     = Charset.defaultCharset();

  /**
   * Our url list
   * Here we find all our urls
   */
  protected List<JavaSitemapGeneratorURL> urls             = new ArrayList<JavaSitemapGeneratorURL>();

  /**
   * The list of available and needed xmlns sitemap entries
   */
  protected List<XMLNS>               xmlns            = new ArrayList<XMLNS>();

  /**
   * The default date format is yyyy-MM-dd
   */
  protected static SimpleDateFormat   simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

  /**
   * Sets the global simple date format
   * 
   * @param simpleDateFormat
   */
  public JavaSitemapGenerator setSimpleDataFormat(SimpleDateFormat simpleDateFormat) {
    if (simpleDateFormat != null)
      AbstractJavaSitemapGenerator.simpleDateFormat = simpleDateFormat;
    return this;
  }

  /**
   * Returns the global simple date format
   * 
   * @return
   * 
   */
  public static SimpleDateFormat getSimpleDateFormat() {
    return AbstractJavaSitemapGenerator.simpleDateFormat;
  }

  /**
   * Adds a url to the sitap generator
   */
  public JavaSitemapGenerator add(JavaSitemapGeneratorURL url) {

    // log
    log.debug("Adding URL: " + url.getLoc().toString());

    // remember url
    this.urls.add(url);

    // remember xmlns
    for (XMLNS xmlns : url.getXMLNS()) {
      addXMLNS(xmlns);
    }

    // return this class
    return this;
  }

  /**
   * Adds a xml namespace
   */
  public void addXMLNS(XMLNS xmlns) {
    if (!this.xmlns.contains(xmlns)) {

      // log
      log.debug("Adding XML namespace: " + xmlns.toString());

      // add xmlns
      this.xmlns.add(xmlns);
    }
  }

  /**
   * Returns the file encoding
   * 
   * @return
   */
  protected String getXMLHeader() {
    return "<?xml version=\"1.0\" encoding=\"" + fileEncoding.displayName() + "\"?>\n";
  }

  /**
   * Returns the number of urls
   * 
   * @return
   */
  public int size() {
    return urls.size();
  }
}
